package com.cn.lyz.viewpagegallery.viewpage_all_response;

import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;

/**
 * Created by yunzhao.liu on 2017/12/6
 */
public class ZoomOutPageTransformer implements ViewPager.PageTransformer {
    public static final float SCALE_LEVEL_DEFAULT = 1f;
    public static final float SCALE_LEVEL_1 = 0.64f;
    public static final float SCALE_LEVEL_2 = 0.45f;

    @Override
    public void transformPage(View page, float position) {
        Log.e("ABC", "position:" + position);
        if (position == 0) {
            page.setScaleX(SCALE_LEVEL_DEFAULT);
            page.setScaleY(SCALE_LEVEL_DEFAULT);
            page.setTranslationX(0f);
            return;
        }
        if (position > 0 && position <= 1) {
            if (position > SCALE_LEVEL_1) {
                position = SCALE_LEVEL_1;
            } else {
                position = Math.max(SCALE_LEVEL_DEFAULT - position, SCALE_LEVEL_1);
            }
            page.setScaleX(position);
            page.setTranslationX(0);
            page.setScaleY(position);
            return;
        }

        if (position > 1 && position < 2) {
            float trans = (page.getWidth() * -(1 - SCALE_LEVEL_2) / 2) * (position-1 );
            if (position - SCALE_LEVEL_DEFAULT > SCALE_LEVEL_2) {
                position = SCALE_LEVEL_2;
            } else {
                position =
                        Math.max(Math.abs(1 + (SCALE_LEVEL_DEFAULT - position)) * SCALE_LEVEL_1,
                                SCALE_LEVEL_2);
            }
            page.setScaleX(position);
            page.setScaleY(position);

            page.setTranslationX(trans);
            return;
        }
        if (position >= 2) {
            page.setScaleX(SCALE_LEVEL_2);
            page.setScaleY(SCALE_LEVEL_2);
            int trans = (int) (page.getWidth() * -(1 - SCALE_LEVEL_2) / 2);
            page.setTranslationX(trans);
            return;
        }

        if (position < 0 && position >= -1) {
            if (-position > SCALE_LEVEL_1) {
                position = SCALE_LEVEL_1;
            } else {
                position = Math.max(SCALE_LEVEL_DEFAULT + position, SCALE_LEVEL_1);
            }
            page.setScaleX(position);
            page.setTranslationX(0);
            page.setScaleY(position);
            return;
        }

        if (position < -1 && position > -2) {
            float trans = -(page.getWidth() * (1 - SCALE_LEVEL_2) / 2) * (position+1 );
            if (-position - SCALE_LEVEL_DEFAULT > SCALE_LEVEL_2) {
                position = SCALE_LEVEL_2;
            } else {
                position =
                        Math.max(Math.abs(1 + (SCALE_LEVEL_DEFAULT + position)) * SCALE_LEVEL_1,
                                SCALE_LEVEL_2);
            }
            page.setScaleX(position);
            page.setScaleY(position);
            page.setTranslationX(trans);
            return;
        }

        if (position <= -2) {
            page.setScaleX(SCALE_LEVEL_2);
            page.setScaleY(SCALE_LEVEL_2);
            int trans = (int) (page.getWidth() * (1 - SCALE_LEVEL_2) / 2);
            page.setTranslationX(trans);
        }

    }
}
